package com.fuqi.android.zhongcai.business.shuig.mvp;

import android.support.annotation.NonNull;
import android.util.Log;

import com.android.volley.Response;
import com.android.volley.error.VolleyError;
import com.fuqi.android.zhongcai.business.shuig.OrderDict;
import com.fuqi.android.zhongcai.business.shuig.request.api.OrderDetailApi;
import com.fuqi.android.zhongcai.business.shuig.request.api.OrderHandleApi;
import com.fuqi.android.zhongcai.business.shuig.request.result.OrderDetailResult;
import com.fuqi.android.zhongcai.business.shuig.request.result.OrderHandleResult;
import com.fuqi.android.zhongcai.business.shuig.view.widget.CJRow.ARResponse;
import com.fuqi.framework.gson.GsonHelper;
import com.fuqi.framework.network.code.HandleCode;
import com.fuqi.framework.network.http.RequestManager;
import com.fuqi.framework.utils.StringUtil;

/**
 * Created by Bruce on 2016/10/20.
 */
public class ShuiGPresenter implements ShuiGContract.Presenter{

    private final ShuiGContract.View shuiGContract;
    String orderid;
    ARResponse r;
    public ShuiGPresenter(String orderid,@NonNull ShuiGContract.View mStatisticsView) {
        this.shuiGContract = mStatisticsView;
        this.orderid = orderid;
        mStatisticsView.setPresenter(this);
    }

    @Override
    public void start() {
        shuiGContract.showDiglog("");
        RequestManager.getInstance().call(new OrderDetailApi(
                new OrderDetailApi.OrderDetailParams(orderid), respOrderDetailListener, errorListener));
    }

    @Override
    public void doWithbohui(String orderjson) {
        shuiGContract.showDiglog("");
        r.update("ORDER_STATUS", OrderDict.StatusType.DDZT_BTG);
        RequestManager.getInstance().call(new OrderHandleApi(
                new OrderHandleApi.OrderHandleParams(r.getDataWithMap()), respListener, errorListener));
    }

    @Override
    public void doWithnoContact(String orderjson) {
        shuiGContract.showDiglog("");
        r.update("ORDER_STATUS", OrderDict.StatusType.DDZT_KHWFLX);
        RequestManager.getInstance().call(new OrderHandleApi(
                new OrderHandleApi.OrderHandleParams(r.getDataWithMap()), respListener, errorListener));
    }

    @Override
    public void doWithljcf(String orderjson) {
        shuiGContract.showDiglog("");
        r.update("ORDER_STATUS", OrderDict.StatusType.DDZT_DAZ);
        RequestManager.getInstance().call(new OrderHandleApi(
                new OrderHandleApi.OrderHandleParams(r.getDataWithMap()), respListener, errorListener));
    }

    @Override
    public void doWithksaz(String orderjson) {
        shuiGContract.showDiglog("");
        r.update("ORDER_STATUS", OrderDict.StatusType.DDZT_AZZ);
        RequestManager.getInstance().call(new OrderHandleApi(
                new OrderHandleApi.OrderHandleParams(r.getDataWithMap()), respListener, errorListener));
    }


    Response.ErrorListener errorListener = new Response.ErrorListener() {

        @Override
        public void onErrorResponse(VolleyError error) {
            shuiGContract.hideDiglog();
            Log.i("juner", "juner  error.getMessage(): " + error.getMessage());
        }
    };

    Response.Listener<OrderDetailResult> respOrderDetailListener = new Response.Listener<OrderDetailResult>() {

        @Override
        public void onResponse(OrderDetailResult response) {
            shuiGContract.hideDiglog();
            if (HandleCode.requestSuccess()) {
                r = new ARResponse(GsonHelper.writeValue(response),"data");
                shuiGContract.show(r);
                shuiGContract.showButton(r);
                shuiGContract.showMenu(r);
                Log.i("juner", "juner OrderDetailResult response : " + GsonHelper.writeValue(response));
            } else {
            }
        }
    };

    Response.Listener<OrderHandleResult> respListener = new Response.Listener<OrderHandleResult>() {

        @Override
        public void onResponse(OrderHandleResult response) {
            shuiGContract.hideDiglog();
            if (HandleCode.requestSuccess()) {
                if(!StringUtil.isEmpty(GsonHelper.writeValue(response))) {
                    r = new ARResponse(GsonHelper.writeValue(response), "data");
                }
                shuiGContract.show(r);
                shuiGContract.showButton(r);
                shuiGContract.showMenu(r);
                Log.i("juner", "juner OrderDetailResult response : " + GsonHelper.writeValue(response));
            } else {
            }
        }
    };
}
